/* Ratings.do (STATA)
	Construct Capital IQ Ratings data.
	by Ralph Koijen & Motohiro Yogo */

#delimit ;
clear all;
set more off;
set type double;

cap log close;
log using Ratings, replace;

/* Define local and global variables */

local directory = "../../Data/Compustat";

do global;


/* Step 1: Prepare default rates */

/* Load data */

u "`directory'/SP Default Rates" if years==10, clear;

drop years;

/* Construct log variables */

gen Ldefault = ln(default);

/* Fit an exponential curve */

reg Ldefault order, robust;

predict Ldefault_pred, xb;

drop Ldefault;

/* Construct fitted default rates */

gen drate = default if order<=4;
replace drate = -exp(Ldefault_pred) if order>4;

drop order default Ldefault_pred;

/* Save data */

tempfile default;
save `default';


/* Step 2: Prepare countries */

u country company_id if !missing(company_id)
	using Countries, clear;

/* Save data */

tempfile Countries;
save `Countries';


/* Step 3: Construct Capital IQ Ratings data */

/* Load data */

u company_id ratingdate ratingtypecode ratingsymbol
	if ratingtypecode=="STDLONG"
	using "`directory'/Capital IQ Entity Ratings", clear;

duplicates drop;

drop ratingtypecode;

/* Merge country code */

merge m:1 company_id using `Countries',
	nogen keep(match);

drop company_id;

/* Rename variables */

rename ratingsymbol rating;

/* Merge default rates */

merge m:1 rating using `default',
	nogen keep(match);

/* Construct variables */

gen int year = year(ratingdate);

gen IratingM = regexm(rating,"^A(\+|-)?$") | regexm(rating,"^BBB(\+|-)?$");
gen IratingJ = regexm(rating,"^BB(\+|-)?$") | regexm(rating,"^B(\+|-)?$") | regexm(rating,"^CC?C?(\+|-)?$");

/* Keep year-end data */

sort country year ratingdate;
by country year: keep if _n==_N;

/* Expand to annual data */

by country: gen byte obs = min(year[_n+1],`=$year_max+1')-year;

expand obs, gen(duplicate);

bysort country year duplicate: replace year = year+_n if duplicate;

drop obs duplicate;

/* Sample criteria */

keep if inrange(year,$year_min,$year_max);

/* Label variables */

order year country;

label var year		"Year";

label var rating	"Rating";
label var drate		"Minus 10-year cumulative average default rate";
label var IratingM	"Dummy: Medium grade";
label var IratingJ	"Dummy: Below investment grade";

/* Save data */

compress;

sort year country;

save Ratings, replace;

log close;
